/* ifd.i */
%module ifd
%#include <memory.h>
%#include <stdlib.h>
// Convert char** to list
%typemap(in) char ** {
     Tcl_Obj **listobjv;
     int       nitems;
     int       i;
     if (Tcl_ListObjGetElements(interp, $input, &nitems, &listobjv) == TCL_ERROR) {
        return TCL_ERROR;
     }
     $1 = (char **) malloc((nitems+1)*sizeof(char *));
     for (i = 0; i < nitems; i++) {
        $1[i] = Tcl_GetStringFromObj(listobjv[i],0);
     }
     $1[i] = 0;
}
// Return a char ** as a Tcl list
%typemap(out) char ** {
        int i = 0;
        while ($1[i]) {
             Tcl_AppendElement(interp,$1[i]);
             i++;
        }
}
// This gives SWIG some cleanup code that will get called after the function call
%typemap(freearg) char ** {
     if ($1) {
        free($1);
     }
}
// Return output argument
%typemap(argout) char *CharPtr {
		int nlgt;
 		Tcl_SetVar(interp,Tcl_GetStringFromObj($input, &nlgt),$1, 0);
}
// Memory new for char*
%typemap(in) char *CharPtr {
     $1 = (char*)(malloc(0xFF*sizeof(char)));
     memset($1,0, strlen($1));
}
// Memory release for char*
%typemap(freearg) char *CharPtr {
     if ($1) {
        free($1);
     }
}
%apply char *CharPtr {char *atr, char *r_apdu}
%{
 extern char **ListIFD(void);
 extern int OpenIFD(char *ifd_name);
 int OpenIFDByID(int ifd_id);
 extern int ResetIFD(void);
 extern int CloseIFD(void);
 extern void GetATR(char *atr);
 void TransmitAPDU(char *c_apdu, char *r_apdu);
%}

%rename (listIFD) ListIFD; 
extern char **ListIFD(void);
%rename (openIFD) OpenIFD;
extern int OpenIFD(char *ifd_name);
%rename (openIFDByID) OpenIFDByID;
int OpenIFDByID(int ifd_id);
%rename (reopenIFD) ResetIFD;
extern int ResetIFD(void);
%rename (closeIFD) CloseIFD;
extern int CloseIFD(void);
%rename (getATR) GetATR;
extern void GetATR(char *atr);
%rename (transmitAPDU) TransmitAPDU;
void TransmitAPDU(char *c_apdu, char *r_apdu);

%include "tclsh.i"